﻿using System;
using System.Windows.Forms;

using DreamCube.WinForm.Common;
using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.TaobaoGJ.UI
{
    public partial class GetSessionKey : Form
    {
        public String UserNick
        {
            private set;
            get;
        }

        public GetSessionKey()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载的时候触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetSessionKey_Load(object sender, EventArgs e)
        {
            try
            {
                this.webBrowser1.Navigate(String.Format(BLL.TB_AppKeyFunc.GetSessionUrl, BLL.TB_AppKeyFunc.AppKey));
                this.webBrowser1.ScriptErrorsSuppressed = true;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
        }

        /// <summary>
        /// 加载完文档触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                UpdateUrl();
                HtmlDocument doc = this.webBrowser1.Document;
                if (doc != null)
                {
                    HtmlElement el = doc.GetElementById(Properties.Resources.ShopTypeElementID);
                    //让页面自动滚动到底部
                    doc.Window.ScrollTo(0, doc.Body.ClientRectangle.Height);
                    if (el != null)
                    {
                        //回调的Url样式http://52icon.net/?top_appkey=21389326&top_parameters=ZXhwaXJlc19pbj04NjQwMCZpZnJhbWU9MSZyMV9leHBpcmVzX2luPTg2NDAwJnIyX2V4cGlyZXNfaW49ODY0MDAmcmVfZXhwaXJlc19pbj04NjQwMCZyZWZyZXNoX3Rva2VuPTYxMDIyMTg0ZWYwZTZhYTA0YjdlMjMxZGVmMWZlYTFmNDk2NTdhMTgwNGI3NWQ4MTgyMzY5NjY4NSZ0cz0xMzg3NDE0NTE3Mzk1JnZpc2l0b3JfaWQ9MTgyMzY5NjY4NSZ2aXNpdG9yX25pY2s9xa7X0Lf+ys4mdzFfZXhwaXJlc19pbj04NjQwMCZ3Ml9leHBpcmVzX2luPTE4MDA=&top_session=6100618fc4916a704e991407581b132dedc284e133021c41823696685&timestamp=2013-12-19+08:55:10&sign=D7d77SxIqOufAutuwhMcBQ==&agreement=true&agreementsign=21389326-23123575-1823696685-79F9EE7C61B8C9B051073EC44D62D659&top_sign=h7ssjfd3QdDlAUPdosgEFQ==
                        String sessionKey = MyString.Left(MyString.RightOfLast(this.webBrowser1.Url.ToString(), "top_session="), "&");
                        String topParameters = MyString.ParseFromBase64(MyString.Left(MyString.RightOfLast(this.webBrowser1.Url.ToString(), "top_parameters="), "&"), System.Text.Encoding.GetEncoding("GBK"));
                        String userNick = MyString.Left(MyString.RightOfLast(topParameters, "visitor_nick="), "&");
                        BLL.SessionKey.SessionNick = userNick;
                        this.UserNick = userNick;
                        BLL.SessionKey.SaveSessionKey(sessionKey, userNick);
                        MessageBox.Show("获取授权成功，窗口关闭后继续您的操作！");
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                MessageBox.Show("获取授权失败，请联系管理员！");
            }
        }

        /// <summary>
        /// 控制好不让浏览器弹出任何窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webBrowser1_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            String url = webBrowser1.Document.ActiveElement.GetAttribute("href");
            if (!String.IsNullOrEmpty(url)) webBrowser1.Navigate(url);
            e.Cancel = true;
        }

        /// <summary>
        /// 跳转页面时，显示Url路径
        /// </summary>
        private void UpdateUrl()
        {
            if (this.webBrowser1.Url != null)
            {
                MyControl.ControlBeginInvoke(this, new Action<String>((url) =>
                {
                    if (MyControl.CanControlInvoke(menuStrip1))
                        toolTxt_Url.Text = url;
                }), this.webBrowser1.Url.ToString());
            }
        }
    }
}
